/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package acousticfield3d.gui.panels;

import acousticfield3d.gui.MainForm;
import acousticfield3d.scene.Entity;
import acousticfield3d.scene.MeshEntity;
import acousticfield3d.scene.Resources;
import acousticfield3d.scene.Scene;
import acousticfield3d.utils.Color;
import acousticfield3d.utils.Parse;
import javax.swing.JCheckBox;
import javax.swing.JTextField;

/**
 *
 * @author Asier
 */
public class MiscPanel extends javax.swing.JPanel {
    public MainForm mf;
    
    public MiscPanel(MainForm mf) {
        this.mf = mf;
        initComponents();
    }

    public boolean isAmpDiscretizer(){
        return ampDicreCheck.isSelected();
    }
    
    public int getAmpDiscretization(){
        return Parse.toInt( ampDiscreText.getText() );
    }
    
    public boolean isPhaseDiscretizer(){
        return phaseDiscreCheck.isSelected();
    }
    
 
    
    public int getPhaseDiscretization(){
        return Parse.toInt( phaseDiscreText.getText() );
    }
    
    public int getTransducersNumber(){
        return Parse.toInt( transducersNumberText.getText() );
    }
   
    public float getFiniteDiffH(){
        return Parse.toFloat( hDivText.getText() );
    }
        
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        maskGroup = new javax.swing.ButtonGroup();
        shadersTypeGroup = new javax.swing.ButtonGroup();
        jPanel3 = new javax.swing.JPanel();
        maskMaskCheck = new javax.swing.JRadioButton();
        maskDisableCheck = new javax.swing.JRadioButton();
        maskVisibleCheck = new javax.swing.JRadioButton();
        maskAddCubeButton = new javax.swing.JButton();
        maskDelButton = new javax.swing.JButton();
        maskAddSphereButton = new javax.swing.JButton();
        colorText = new javax.swing.JTextField();
        phaseDiscreCheck = new javax.swing.JCheckBox();
        phaseDiscreText = new javax.swing.JTextField();
        reloadShadersButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        transducersNumberText = new javax.swing.JTextField();
        ampDicreCheck = new javax.swing.JCheckBox();
        ampDiscreText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        hDivText = new javax.swing.JTextField();

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("mask objects"));

        maskGroup.add(maskMaskCheck);
        maskMaskCheck.setText("mask");
        maskMaskCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maskMaskCheckActionPerformed(evt);
            }
        });

        maskGroup.add(maskDisableCheck);
        maskDisableCheck.setSelected(true);
        maskDisableCheck.setText("disable");
        maskDisableCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maskDisableCheckActionPerformed(evt);
            }
        });

        maskGroup.add(maskVisibleCheck);
        maskVisibleCheck.setText("visible");
        maskVisibleCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maskVisibleCheckActionPerformed(evt);
            }
        });

        maskAddCubeButton.setText("Cube");
        maskAddCubeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maskAddCubeButtonActionPerformed(evt);
            }
        });

        maskDelButton.setText("Del");
        maskDelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maskDelButtonActionPerformed(evt);
            }
        });

        maskAddSphereButton.setText("Sphe");
        maskAddSphereButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maskAddSphereButtonActionPerformed(evt);
            }
        });

        colorText.setText("255,255,255,255");
        colorText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colorTextActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(colorText)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(maskDisableCheck)
                            .addComponent(maskAddCubeButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(maskVisibleCheck)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(maskMaskCheck))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(maskAddSphereButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(maskDelButton)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maskDisableCheck)
                    .addComponent(maskVisibleCheck)
                    .addComponent(maskMaskCheck))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maskAddCubeButton)
                    .addComponent(maskAddSphereButton)
                    .addComponent(maskDelButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(colorText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        phaseDiscreCheck.setText("Phase discretization (pi):");

        phaseDiscreText.setText("10");

        reloadShadersButton.setText("ReloadShaders");
        reloadShadersButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reloadShadersButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Number of transducers:");

        transducersNumberText.setText("64");

        ampDicreCheck.setText("Amp discretization:");

        ampDiscreText.setText("10");

        jLabel2.setText("hdiv (w):");

        hDivText.setText("5");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(transducersNumberText))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(ampDicreCheck, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(phaseDiscreCheck, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(phaseDiscreText)
                            .addComponent(ampDiscreText)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(hDivText))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(reloadShadersButton)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ampDicreCheck)
                    .addComponent(ampDiscreText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phaseDiscreCheck)
                    .addComponent(phaseDiscreText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(transducersNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(hDivText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(reloadShadersButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void maskMaskCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maskMaskCheckActionPerformed
        Scene.setVisible(mf.scene.getEntities(), Entity.TAG_MASK, true);
        Scene.setShader(mf.scene.getEntities(), Entity.TAG_MASK, Resources.SHADER_MASK);
        mf.needUpdate();
    }//GEN-LAST:event_maskMaskCheckActionPerformed

    private void maskDisableCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maskDisableCheckActionPerformed
        Scene.setVisible(mf.scene.getEntities(), Entity.TAG_MASK, false);
        mf.needUpdate();
    }//GEN-LAST:event_maskDisableCheckActionPerformed

    private void maskVisibleCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maskVisibleCheckActionPerformed
        Scene.setVisible(mf.scene.getEntities(), Entity.TAG_MASK, true);
        Scene.setShader(mf.scene.getEntities(), Entity.TAG_MASK, Resources.SHADER_SOLID_SPEC);
        mf.needUpdate();
    }//GEN-LAST:event_maskVisibleCheckActionPerformed

    private void maskAddCubeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maskAddCubeButtonActionPerformed
        MeshEntity me = new MeshEntity(Resources.MESH_BOX, null, Resources.SHADER_SOLID_SPEC);
        me.setTag( Entity.TAG_MASK ); me.setColor( Color.WHITE );
        mf.addMeshEntityToSceneCenter(me);
        mf.needUpdate();
    }//GEN-LAST:event_maskAddCubeButtonActionPerformed

    private void maskDelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maskDelButtonActionPerformed
        //Remove from simulation and scene
        for (Entity e : mf.selection){
            if (e.getTag() == Entity.TAG_MASK){
                mf.scene.getEntities().remove( e );
            }
        }
        mf.clearSelection();
        mf.needUpdate();
    }//GEN-LAST:event_maskDelButtonActionPerformed

    private void maskAddSphereButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maskAddSphereButtonActionPerformed
        MeshEntity me = new MeshEntity(Resources.MESH_SPHERE, null, Resources.SHADER_SOLID_SPEC);
        me.setTag( Entity.TAG_MASK ); me.setColor( Color.WHITE );
        mf.addMeshEntityToSceneCenter(me);
        mf.needUpdate();
    }//GEN-LAST:event_maskAddSphereButtonActionPerformed

    private void reloadShadersButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reloadShadersButtonActionPerformed
        String templateFile = Resources.TEMPLATE_SHADERS_STENCILDIR;
        Resources.get().initTemplates(templateFile);
        mf.renderer.reloadShaders();
    }//GEN-LAST:event_reloadShadersButtonActionPerformed

    private void colorTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colorTextActionPerformed
        int color = Color.parse( colorText.getText() );
        for (Entity e : mf.selection){
            if (e instanceof MeshEntity && e.getTag() == Entity.TAG_MASK){
                MeshEntity me = (MeshEntity)e;
                me.setColor( color );
            }
        }
        mf.needUpdate();
    }//GEN-LAST:event_colorTextActionPerformed

    public JCheckBox getAmpDicreCheck() {
        return ampDicreCheck;
    }

    public JTextField getAmpDiscreText() {
        return ampDiscreText;
    }

    public JCheckBox getPhaseDiscreCheck() {
        return phaseDiscreCheck;
    }

    public JTextField getPhaseDiscreText() {
        return phaseDiscreText;
    }

    

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox ampDicreCheck;
    private javax.swing.JTextField ampDiscreText;
    private javax.swing.JTextField colorText;
    private javax.swing.JTextField hDivText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton maskAddCubeButton;
    private javax.swing.JButton maskAddSphereButton;
    private javax.swing.JButton maskDelButton;
    private javax.swing.JRadioButton maskDisableCheck;
    private javax.swing.ButtonGroup maskGroup;
    private javax.swing.JRadioButton maskMaskCheck;
    private javax.swing.JRadioButton maskVisibleCheck;
    private javax.swing.JCheckBox phaseDiscreCheck;
    private javax.swing.JTextField phaseDiscreText;
    private javax.swing.JButton reloadShadersButton;
    private javax.swing.ButtonGroup shadersTypeGroup;
    private javax.swing.JTextField transducersNumberText;
    // End of variables declaration//GEN-END:variables
}
